<?php
/**
 * Created by IntelliJ IDEA.
 * User: vbolshov
 * Date: 01.10.2010
 * Time: 11:02:58
 */

namespace km\filter;

class InputFilter {
	/**
	 * a ref to one of superglobals
	 * @var array
	 */
	private $source;

	function __construct($type)
	{
		switch ($type) {
			case INPUT_POST:
		        $this->source = & $_POST;
		        break;
			case INPUT_GET:
		        $this->source = & $_GET;
		        break;
			case INPUT_REQUEST:
		        $this->source = & $_REQUEST;
		        break;
			case INPUT_COOKIE:
		        $this->source = & $_GET;
		        break;
			case INPUT_SERVER:
		        $this->source = & $_GET;
		        break;
			default:
		        throw new InvalidArgumentException("Input type not supported");
		        break;
		}
	}
	function int($key, $min = null, $max = null, $flags = null)
	{
		return Filter::int($this->val($key), $min, $max, $flags);
	}
	function intArray($key, $min = null, $max = null)
	{
		return Filter::intArray($this->val($key), $min, $max);
	}
	function positiveInt($key)
	{
		return Filter::positiveInt($this->val($key));
	}
	function positiveIntArray($key)
	{
		return Filter::positiveIntArray($this->val($key));
	}
	function raw($key)
	{
		return Filter::raw($this->val($key));
	}
	function str($key, $min = null, $max = null)
	{
		return Filter::str($this->val($key));
	}
	function callback($key, $cb)
	{
		return Filter::callback($this->val($key), $cb);
	}
	private function val($key)
	{
		return isset($this->source[$key]) ? $this->source[$key] : null;
	}
}
